package com.baizhi.service.impl;

import com.baizhi.common.CodeException;
import com.baizhi.entity.CmfzAdmin;
import com.baizhi.mapper.CmfzAdminMapper;
import com.baizhi.mapper.CmfzRoleMapper;
import com.baizhi.service.CmfzAdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljh
 * @since 2020-08-18
 */

@Service
public class CmfzAdminServiceImpl extends ServiceImpl<CmfzAdminMapper, CmfzAdmin> implements CmfzAdminService {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private CmfzRoleMapper cmfzRoleMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public UserDetails loadUserByUsername(String username) throws RuntimeException{
        System.out.println("--------------------------");
        String code = request.getParameter("code");
        HttpSession session = request.getSession();
        String realCode = (String) session.getAttribute("realCode");
        if (!realCode.equals(code)){
            throw new CodeException("验证码错误");
        }
        CmfzAdmin admin = selectByUsername(username);
        return admin;
    }


    @Override
    public CmfzAdmin selectByUsername(String username) {
        QueryWrapper<CmfzAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        CmfzAdmin admin = baseMapper.selectOne(queryWrapper);

        /*if (admin!=null){
            Set<String> allRoles = cmfzRoleMapper.getAllRolesByUsername(username);

            Set<String> allPermissions = cmfzRoleMapper.getAllPermissionsByUsername(username);
            //2.封装到admin中
            List<GrantedAuthority> list = new ArrayList<>();

            for (String role : allRoles) {
                list.add(new SimpleGrantedAuthority(role));
            }

            for (String permission : allPermissions) {
                list.add(new SimpleGrantedAuthority(permission));
            }

            admin.setAuthorities(list);
        }*/
        return admin;
    }

    @Override
    public PageInfo<CmfzAdmin> selectAllAdmin(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<CmfzAdmin> cmfzAdmins = baseMapper.selectList(null);
        return new PageInfo<CmfzAdmin>(cmfzAdmins);
    }

    @Override
    public void addAdmin(String username, String password, List<Integer> roleIds) {
        String encode = bCryptPasswordEncoder.encode(password);
        CmfzAdmin cmfzAdmin = new CmfzAdmin();
        cmfzAdmin.setUsername(username);
        cmfzAdmin.setPassword(encode);

        baseMapper.insert(cmfzAdmin);

        for (Integer roleId : roleIds) {
            baseMapper.addRoleToUser(cmfzAdmin.getId(),roleId);
        }
    }

    @Override
    public void deleteAdmin(Integer id) {
        //删除管理员同时删除管理员与角色中间表中的数据
        baseMapper.deleteAdminAndRoleByAdminId(id);
        baseMapper.deleteById(id);
    }
}
